import java.util.Scanner;

public class ControlPanel {
    // 定义ControlPanel的状态
    enum State {
        READING_INPUT, LOCKED, FUNCTION_SELECTION
    }

    private State state = State.READING_INPUT; // 初始状态为读取输入
    private String enteredPassword = ""; // 用户输入的密码
    private int wrongAttempts = 0; // 错误尝试次数
    private long lastLockedTime = 0; // 最后锁定时间

    public void inputCharacter(char character) {
        if (state == State.LOCKED) {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - lastLockedTime) > 120000) { // 检查是否超过120秒
                state = State.READING_INPUT;
                wrongAttempts = 0; // 重置错误尝试次数
                enteredPassword = ""; // 清空已输入的密码
                System.out.println("Control panel unlocked. Please enter password.");
            }
            return;
        }

        if (state == State.READING_INPUT) {
            enteredPassword += character;
            System.out.println("Character entered: " + character);
            if (enteredPassword.length() == 4) { // 密码长度为4
                if ("1234".equals(enteredPassword)) { // 假设正确密码为"1234"
                    state = State.FUNCTION_SELECTION;
                    System.out.println("Password correct. Entering function selection state.");
                } else {
                    wrongAttempts++;
                    if (wrongAttempts == 3) {
                        state = State.LOCKED;
                        lastLockedTime = System.currentTimeMillis();
                        System.out.println("Password incorrect 3 times. Control panel locked for 120 seconds.");
                    } else {
                        System.out.println("Password incorrect. Please try again.");
                        enteredPassword = ""; // 重置已输入的密码
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ControlPanel controlPanel = new ControlPanel();

        System.out.println("Please enter your 4-digit password:");
        while (true) {
            char[] input = scanner.nextLine().toCharArray();
            if (input.length == 4) { // 确保输入了4个字符
                for (char c : input) {
                    controlPanel.inputCharacter(c);
                    if (controlPanel.state == State.FUNCTION_SELECTION || controlPanel.state == State.LOCKED) {
                        break;
                    }
                }
            } else {
                System.out.println("Invalid input. Please enter exactly 4 characters.");
            }

            if (controlPanel.state == State.LOCKED) {
                try {
                    Thread.sleep(130000); // 等待超过120秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else if (controlPanel.state == State.FUNCTION_SELECTION) {
                break;
            }
        }
        scanner.close();
    }
}